package com.diandian.backend.product.service;

import com.diandian.backend.product.resp.ProductOfflineInfoPictureVideoListBusinessResp;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import com.diandian.backend.product.domain.ProductOfflineInfoPictureVideo;
import com.diandian.backend.product.dto.*;
import com.diandian.backend.product.mapper.ProductOfflineInfoPictureVideoMapper;
import com.diandian.backend.product.resp.ProductOfflineInfoPictureVideoDetailResp;
import com.diandian.backend.product.resp.ProductOfflineInfoPictureVideoPageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.diandian.backend.product.resp.ProductOfflineInfoPictureVideoListResp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProductOfflineInfoPictureVideoService {

    @Autowired
    private ProductOfflineInfoPictureVideoMapper productOfflineInfoPictureVideoMapper;

    public PageResult page(ProductOfflineInfoPictureVideoPageParamDTO requestDTO){

        requestDTO.check();
        int pageNum = requestDTO.getPageNum();
        int pageSize = requestDTO.getPageSize();

        // 查询总数
        int totalCount = productOfflineInfoPictureVideoMapper.getCount(requestDTO);

        // 查询数据集合
        List<ProductOfflineInfoPictureVideo> dataList = productOfflineInfoPictureVideoMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<ProductOfflineInfoPictureVideoPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                ProductOfflineInfoPictureVideoPageResp item = new ProductOfflineInfoPictureVideoPageResp();
                item.setId(x.getId());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUesrName(x.getCreateUesrName());
                item.setCreateTime(x.getCreateTime());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                item.setSourceType(x.getSourceType());
                item.setSourceId(x.getSourceId());
                item.setDataType(x.getDataType());
                item.setFileType(x.getFileType());
                item.setUrl(x.getUrl());
                item.setRecommend(x.getRecommend());
                item.setSequence(x.getSequence());
             respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public ProductOfflineInfoPictureVideoListBusinessResp getBusinessList(ProductOfflineInfoPictureVideoListParamDTO requestDTO){
        List<ProductOfflineInfoPictureVideoListResp> allList = getList(requestDTO);
        if(allList == null || allList.size() == 0){
            return new ProductOfflineInfoPictureVideoListBusinessResp();
        }

        ProductOfflineInfoPictureVideoListBusinessResp result = new ProductOfflineInfoPictureVideoListBusinessResp();
        List<ProductOfflineInfoPictureVideoListResp> productCovers = new ArrayList<>();
        List<ProductOfflineInfoPictureVideoListResp> productDetails = new ArrayList<>();
        List<ProductOfflineInfoPictureVideoListResp> productVideos = new ArrayList<>();
        List<ProductOfflineInfoPictureVideoListResp> productCertificates = new ArrayList<>();
        List<ProductOfflineInfoPictureVideoListResp> productSpecials = new ArrayList<>();
        for (int i = 0; i < allList.size(); i++) {
            ProductOfflineInfoPictureVideoListResp item = allList.get(i);
            if("0".equals(item.getDataType()) && "0".equals(item.getFileType())){
                productCovers.add(item);
                continue;
            }
            if("1".equals(item.getDataType()) && "1".equals(item.getFileType())){
                productVideos.add(item);
                continue;
            }
            if("1".equals(item.getDataType())){
                productDetails.add(item);
            }
            if("2".equals(item.getDataType())){
                productCertificates.add(item);
            }
            if("3".equals(item.getDataType())){
                productSpecials.add(item);
            }
        }

        result.setProductCovers(productCovers);
        result.setProductDetails(productDetails);
        result.setProductVideos(productVideos);
        result.setProductCertificates(productCertificates);
        result.setProductSpecials(productSpecials);

        return result;
    }

    public List<ProductOfflineInfoPictureVideoListResp> getList(ProductOfflineInfoPictureVideoListParamDTO requestDTO){

        requestDTO.check();

        // 查询数据集合
        List<ProductOfflineInfoPictureVideo> dataList = productOfflineInfoPictureVideoMapper.getList(requestDTO);

        // 组装数据
        List<ProductOfflineInfoPictureVideoListResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                ProductOfflineInfoPictureVideoListResp item = new ProductOfflineInfoPictureVideoListResp();
                item.setId(x.getId());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUesrName(x.getCreateUesrName());
                item.setCreateTime(x.getCreateTime());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                item.setBelong(x.getBelong());
                item.setProductCode(x.getProductCode());
                item.setSourceType(x.getSourceType());
                item.setSourceId(x.getSourceId());
                item.setDataType(x.getDataType());
                item.setFileType(x.getFileType());
                item.setUrl(x.getUrl());
                item.setRecommend(x.getRecommend());
                item.setSequence(x.getSequence());
                respList.add(item);
            });
        }

        return respList;
    }

    public ProductOfflineInfoPictureVideoDetailResp getDetail(ProductOfflineInfoPictureVideoDetailParamDTO param) {

        ProductOfflineInfoPictureVideo dbData = productOfflineInfoPictureVideoMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        ProductOfflineInfoPictureVideoDetailResp result = new ProductOfflineInfoPictureVideoDetailResp();
        result.setId(dbData.getId());
        result.setCreateUserId(dbData.getCreateUserId());
        result.setCreateUesrName(dbData.getCreateUesrName());
        result.setCreateTime(dbData.getCreateTime());
        result.setUpdateUserId(dbData.getUpdateUserId());
        result.setUpdateUserName(dbData.getUpdateUserName());
        result.setUpdateTime(dbData.getUpdateTime());
        result.setDel(dbData.getDel());
        result.setSourceType(dbData.getSourceType());
        result.setSourceId(dbData.getSourceId());
        result.setDataType(dbData.getDataType());
        result.setFileType(dbData.getFileType());
        result.setUrl(dbData.getUrl());
        result.setRecommend(dbData.getRecommend());
        result.setSequence(dbData.getSequence());
        return result;
    }

    public void updateById(ProductOfflineInfoPictureVideoUpdateByIdParamDTO param) {
        param.check();

        ProductOfflineInfoPictureVideoDetailParamDTO queryDetailDTO = new ProductOfflineInfoPictureVideoDetailParamDTO();
        queryDetailDTO.setId(param.getId());
        ProductOfflineInfoPictureVideo dbData = productOfflineInfoPictureVideoMapper.getDetail(queryDetailDTO);
        if(dbData == null){
            throw new DefaultException("更新的记录不存在");
        }

        productOfflineInfoPictureVideoMapper.updateById(param);
    }

    public void deleteById(ProductOfflineInfoPictureVideoDeleteByIdParamDTO paramDTO) {
        paramDTO.check();
        productOfflineInfoPictureVideoMapper.deleteById(paramDTO);
    }

    /**
     * 删除
     * @param belong
     * @param sourceType 来源类型：0 商品 1 sku
     * @param sourceId 来源数据id，对应商品编码或者sku编码
     * @param dataType 数据类型：0 一般信息 1 资质信息 2 特殊信息
     * @param fileType 文件类型：0 图片 1 商品
     */
    public void deleteByCode(String belong, String sourceType, String sourceId, String dataType, String fileType) {
        productOfflineInfoPictureVideoMapper.deleteByCode(belong, sourceType, sourceId, dataType, fileType);
    }

    public void deleteByProductCode(String belong, String productCode) {
        productOfflineInfoPictureVideoMapper.deleteByProductCode(belong, productCode);
    }

    /**
     * 删除
     * @param belong
     * @param sourceType 来源类型：0 商品 1 sku
     * @param sourceId 来源数据id，对应商品编码或者sku编码
     * @param dataType 数据类型：0 一般信息 1 资质信息 2 特殊信息
     * @param fileType 文件类型：0 图片 1 商品
     */
    public void deleteByCodeIn(String belong, String sourceType, List<String> sourceId, String dataType, String fileType) {
        productOfflineInfoPictureVideoMapper.deleteByCodeIn(belong, sourceType, sourceId, dataType, fileType);
    }

    public void add(ProductOfflineInfoPictureVideoAddParamDTO param) {
        productOfflineInfoPictureVideoMapper.add(param);
    }

    public void batchAdd(List<ProductOfflineInfoPictureVideoAddParamDTO> dataList) {
        if(dataList == null || dataList.size() == 0){
            return;
        }
        dataList.forEach(x -> {
            x.setCreateTime(new Date());
            x.setDel(0);
        });
        productOfflineInfoPictureVideoMapper.batchAdd(dataList);
    }
}
